from django_filters import *
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework.filters import SearchFilter, OrderingFilter

from iClouds.apidev import hash_md5
from mysqldb.models import *


class ListDjangoFilterBackend(DjangoFilterBackend):
    """自己定义list过滤器"""

    def get_schema_fields(self, view):
        if view.action not in ["list"]:
            return []
        return super().get_schema_fields(view)


class AllinpayMemberListFilterSet(FilterSet):
    """过滤器"""

    pid = NumberFilter(field_name="pid", help_text="pid")
    phone = CharFilter(field_name="phone", help_text="手机号")
    idcode = CharFilter(field_name="idcode", method="hash_idcode", help_text="身份证号")

    class Meta:
        model = AllinpayMemberList
        fields = ["pid", "phone", "idcode"]

    def hash_idcode(self, qs, name, value):
        return qs.filter(**{name: hash_md5(value.upper())})
